﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoundBottpmFlask : BaseBrokenObj
{
    public GameObject m_Target;
    public Transform m_FatherTarget;
    public Transform m_BigFather;
    public GameObject m_FlaskWater;
    public GameObject m_FlaskWater2;
    public RubberStopper m_RubberStopper;
    public GameObject m_RubberDropper;//
    public GameObject m_Effect;
    public RubberDropper2 m_RubberDropper2;


    Animator m_Animator;
    public static bool HaveWater;
    [HideInInspector]
    public static bool HaveStopper;
    [HideInInspector]
    public bool m_IsStart;
    [HideInInspector]
    public bool m_HaveReadWater;//
    [HideInInspector]
    public bool m_HaveAQ = true;//有氨气

    public static RoundBottpmFlask Instance;

    private void Awake()
    {
        Instance = this;
    }

    public void Start()
    {
        base.Start();
        m_Animator = m_FlaskWater.GetComponent<Animator>();
    }

    [HideInInspector]
    public bool OnPlatfrom;
    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        if (!OnPlatfrom)
        {
            base.OnReleseTrigger(handleType);
            transform.SetParent(m_BigFather);
        }
        else
        {
            float z = transform.eulerAngles.z;
            if (z > 100 && z <= 260)
            {
                SetOnPlatfrom();//放置到铁架台上
            }
            else
            {
                base.OnReleseTrigger(handleType);
                transform.SetParent(m_BigFather);
            }
        }
        isCatch = false;
    }
    public override void BrokenEffOnRestoreEvent()
    {
        base.BrokenEffOnRestoreEvent();
        m_RubberStopper.OnRoundBottpmFlask();
    }

    public void SetOnGround()
    {
        m_RubberDropper.transform.localScale = new Vector3(1f, 1f, 1f);
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
        transform.SetParent(m_BigFather);
    }

    public void ResetParent()
    {
        transform.SetParent(m_BigFather);
    }

    public void SetOnPlatfrom()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        transform.position = m_Target.transform.position;
        transform.SetParent(m_FatherTarget);
        StepsManager.Instance.StepTwoNum[0] = true;
        StepsManager.Instance.SetStepTwo(0);
        Round(180);
    }

    public void Round(int num)
    {//旋转圆底烧瓶
        transform.rotation = Quaternion.Euler(0, 0, num);
    }

    public void SetStartMove()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
    }

    public void SetStopMove()
    {
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
    }

    private void Update()
    {
        if (NoloVR.NoloVRInput.singleton.OnNoloLeftTouchDown() || NoloVR.NoloVRInput.singleton.OnNoloRightTouchDown())
        {
            if (!OnPlatfrom || !RubberStopperDoubleKo.Instance.OnFlask)
            {

            }
            else
            {
                if (RubberStopperDoubleKo.Instance.m_InWater && m_RubberDropper2.isHandle)
                    StartAction();
            }
        }

        float z = transform.eulerAngles.z;
        if ((z > 90 && z <= 180) || (z < 270 && z > 180))
        {
            if (isCatch && !haveStopper && StepsManager.Instance.StepFourNum[1])
            {
                StepsManager.Instance.StepFiveNum[0] = true;
                StepsManager.Instance.SetStepFive(0);
                m_FlaskWater.SetActive(false);
                m_FlaskWater2.SetActive(false);
                m_HaveReadWater = false;

            }
        }
    }

    bool m_CanSetRubberStopper = true;
    private void LateUpdate()
    {
        if (Vector3.Distance(m_Target.transform.position, transform.position) < 0.1f)
        {
            OnPlatfrom = true;
        }
        else
        {
            OnPlatfrom = false;
        }

        if (!m_collider.gameObject.activeInHierarchy)
        {
            if (!m_CanSetRubberStopper) return;
            m_CanSetRubberStopper = false;
            m_RubberStopper.ResetParent();
            m_RubberStopper.OnGround();
        }
        else
        {
            m_CanSetRubberStopper = true;
        }
    }

    public void StartAction()
    {
        if (!m_HaveAQ) return;
        canCatsh = false;
        m_HaveReadWater = true;
        HaveWater = true;
        if (HaveWater)
        {
            StepsManager.Instance.StepThreeNum[0] = true;
            StepsManager.Instance.SetStepThree(0);
        }
        if (WaterStopClip.OpenJiaZi)
        {
            m_IsStart = true;
            m_Effect.SetActive(true);
            m_RubberDropper.transform.localScale = new Vector3(0.2f, 1, 1);
            if (Beaker.m_FtNum >= 1)
            {
                m_FlaskWater.SetActive(true);
            }
            else
            {
                m_FlaskWater2.SetActive(true);
            }
            Invoke("StopAction", 12f);
        }
        else
        {
            m_RubberDropper.transform.localScale = new Vector3(0.2f, 1, 1);
            Invoke("ResetRubberDropper", 1f);
        }
    }

    void ResetRubberDropper()
    {
        m_RubberDropper.transform.localScale = new Vector3(1f, 1, 1);
    }

    private void StopAction()
    {
        m_HaveAQ = false;
        m_Effect.SetActive(false);
        m_RubberDropper.transform.localScale = new Vector3(1f, 1f, 1f);
        canCatsh = true;
        m_IsStart = false;
        StepsManager.Instance.StepThreeNum[2] = true;
        StepsManager.Instance.SetStepThree(2);
    }


    bool haveStopper;
    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "Rubberstopper")
        {
            haveStopper = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "Rubberstopper")
        {
            if (m_IsOnGround)
            {
                m_HaveAQ = false;
            }
            haveStopper = false;
        }
    }


    //记录每个大步骤的初始状态
    #region
    public void SetBigStepTwo()
    {
        ResetPosition();
        m_FlaskWater.SetActive(false);
        transform.SetParent(m_BigFather);
        HaveWater = false;
        m_HaveAQ = true;
        m_HaveReadWater = false;
    }

    public void SetBigStepThree()
    {
        Round(180);
        SetOnPlatfrom();
        m_FlaskWater.SetActive(false);
        HaveWater = false;
        m_HaveAQ = true;
    }

    public void SetBigStepFour()
    {
        Round(180);
        SetOnPlatfrom();
        m_FlaskWater.SetActive(true);
        m_Animator.speed = 100f;
        Invoke("ResteSpees", 1f);
        HaveWater = true;
    }

    public void SetBigStepFive()
    {
        ResetPosition();
        m_FlaskWater.SetActive(true);
        m_Animator.speed = 100f;
        Invoke("ResteSpees", 1f);
    }
    #endregion

    void ResteSpees()
    {
        m_Animator.speed = 1f;
    }
    public bool m_IsOnGround;
    private void OnCollisionStay(Collision other)
    {
        if (other.gameObject.name == "Ground")
        {
            m_IsOnGround = true;
        }
    }

    private void OnCollisionExit(Collision other)
    {
        if (other.gameObject.name == "Ground")
        {
            m_IsOnGround = false;
        }
    }
}
